// pages/personal/networkData/networkData.js
var app = getApp();
var api = require('../../../utils/api.js');
var apis = api.api;
var util = require('../../../utils/util.js')
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		type: null, //判断角色是什么权限
		userWallet: {}, //用户的余额信息
		shopName: '', //用户的名称
		page: 1, //余额页数
		payData: [], //余额支出数据
		withdrawData: [], //提现记录
		titlea: "",
		isBottom: false, //判断是否还有数据
		isBot: false,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	goQrcode() {
		wx.navigateTo({
			url: '/pages/personal/myPromote/qrcode/qrcode?type=usermore' + this.data.type
		})
	},
	goQrcodeOrder() {
		wx.navigateTo({
			url: '/pages/personal/myPromote/qrcode/qrcode?type=traderorder'
		})
	},
	getTitle() {
		// 账户类型    1代表用户   2代表代驾  3代表省代理 4代表市代理
		// 5代表区代理  6代表围栏代理  7代表网约车司机  8代表网约车用户  
		// 9代表骑手  10代表商家  11代表外卖用户  12业务    13代驾商户
		let type = Number(this.data.type);
		if (type === 1) {
			return "用户";
		} else if (type === 2) {
			return "代驾";
		} else if (type === 3) {
			return "省代理";
		} else if (type === 4) {
			return "市代理";
		} else if (type === 5) {
			return "区代理";
		} else if (type === 6) {
			return "网点";
		} else if (type === 7) {
			return "网约车司机";
		} else if (type === 8) {
			return "网约车用户";
		} else if (type === 9) {
			return "骑手";
		} else if (type === 10) {
			return "外卖商家";
		} else if (type === 11) {
			return "外卖用户";
		} else if (type === 12) {
			return "业务";
		} else if (type === 13) {
			return "代驾商户";
		}
	},
	initTitle() {
		wx.setNavigationBarTitle({
			title: this.getTitle() + "数据",
			success(res) {},
			fail(res) {}
		})
		this.setData({
			titlea: this.getTitle() + "数据"
		})
	},
	onLoad: function(options) {
		var type = options.type;
		this.setData({
			type: options.type
		})
		this.initTitle();
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		var that = this;
		that.getPayList(that.data.type)
		var userWallets = wx.getStorageSync('userAllInfo').wallet
		var userInfo = wx.getStorageSync('userAllInfo')
		if (that.data.type == 6) {
			that.setData({
				shopName: userInfo.wangdian.name
			})
		} else if (that.data.type == 4) {
			that.setData({
				shopName: userInfo.city.name
			})
		} else if (that.data.type == 3) {
			that.setData({
				shopName: userInfo.province.name
			})
		} else {
			that.setData({
				shopName: userInfo.username
			})
		}
		userWallets.map((item, index) => {
			if (item.type == that.data.type) {
				that.data.userWallet = item;
			}
		})
		if (JSON.stringify(that.data.userWallet) === '{}') {
			var userWallet = {
				id: 0,
				user_id: userInfo.id,
				type: that.data.type,
				account: '0.00',
				score: 0,
				commission: '0.00'
			}
			that.setData({
				userWallet: userWallet
			})
		}
		app.messageEvent = function(event, data) {
			/**
			 * @var string event	事件名称
			 * @var object data	返回的数据
			 */
			switch (event) {
				case "user_wallet_change":
					let userdata = wx.getStorageSync('userAllInfo'),
						iswallet;
					for (var i in userdata.wallet) {
						if (Number(userdata.wallet[i].type) === Number(data.type)) {
							userdata.wallet[i] = data;
							iswallet = true;
						}
					}
					if (!iswallet) {
						userdata.wallet.push(data);
					}
					if (Number(that.data.type) === Number(data.type)) {
						that.setData({
							userWallet: data
						})
					}
					wx.setStorageSync('userAllInfo', userdata)
					return true;
					break;
			}
		}

		that.setData({
			userWallet: that.data.userWallet
		})
	},
	getPayList(type) {
		// 获取用户余额明细
		var that = this;
		var url = apis.host + 'index/accountLog'
		var data = {
			type: '0',
			/* 否	string	类型 默认0 全部 1支出 2收入 */
			page: that.data.page,
			/* 否	string	页码 */
			size: '10',
			/* 是	string	页额 */
			user_type: type,
			/* 是	string	用户类型 */
			money_type: 'commission',
		}
		wx.showLoading({
			title: '正在加载中'
		})
		apis.request(url, data, 'POST').then(function(res) {
			if (res.code == 200) {
				wx.hideLoading()
				if (res.data.length == 0) {
					that.setData({
						isBottom: true
					})
					return
				}
				res.data.map((item, index) => {
					item.create_at = util.formatTimeTwo(item.create_at, 'Y年M月D日 h:m:s')
				})
				// {
				// 	"id": 2,
				// 	"user_id": 7,
				// 	"type": "order",
				// 	"type_cz": 1,
				// 	"price": "100.00",
				// 	"title": "出车收入",
				//  "brief": "测试收入第一环",
				// 	"create_at": 1637570554,
				// 	"account_type": 2
				// }
				that.setData({
					payData: [...that.data.payData, ...res.data]
				})
			}
		})
	},
	onChange(e) {
		//切换tab按钮
		var that = this;
		if (e.detail.index == 0) {
			that.getPayList(that.data.type)
		} else {
			that.getWithdraw(that.data.type)
		}
	},
	goTx() {
		//前往提现页面
		wx.navigateTo({
			url: '/pages/personal/myAccount/withdrawal/withdrawal?type=' + this.data.type
		})
	},
	getWithdraw(type) {
		// https://dageda.api.hjdz.ltd/index/withdrawInfo
		var that = this;
		var url = apis.host + 'index/withdrawInfo'
		var data = {
			page: that.data.page,
			/* 否	string	页码 */
			size: '10',
			/* 是	string	页额 */
			type: type,
			/* 是	string	用户类型 */
		}
		apis.request(url, data, 'POST').then(function(res) {
			if (res.code == 200) {
				// res.data.map((item, index) => {
				// 	var date = new Date();
				// 	console.log('date', date)
				// 	item.create_at = util.formatTimeTwo(date)
				// })
				that.setData({
					withdrawData: res.data
				})
			}
		})
	},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		this.setData({
			page: this.data.page + 1
		})
		this.getPayList(this.data.type)
	},
	lower() {
		this.setData({
			page: this.data.page + 1
		})
		this.getPayList(this.data.type)
	},
})
